#include <iostream>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
using namespace std;

int main()
{
  pid_t pd = fork();
  if (pd == 0)
  {
     // 子进程
     int cunt = 5;
     while (cunt)
     {
     cout << "子进程；" << cunt << endl;
       cunt--; 
       sleep(1);
     }
     exit(11);
  }else
  {
    // 父进程
    
    int quit = 100;
     while (!quit)
    {
     int status = 100;
     pid_t ret_pd = waitpid(-1, &status, WNOHANG);
     if (ret_pd > 0)
     {
      cout << "等待子进程成功，子进程退出成功" << endl;
      cout << "获取退出码:" << WEXITSTATUS(status) << endl;
     }else if(ret_pd == 0)
     {
      cout << "等待成功，子进程让我们先玩会" << endl;
      cout << "获取退出码:" << WEXITSTATUS(status) << endl;
     }else 
     {
      cout << "等待失败，子进程异常退出" <<endl; 
      quit = 1;
    }
     sleep(1);
    } 
  }
  return 0;
}





